package com.gao.api.gate.quick.proto.config;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;

/**
 * <p></p>
 *
 * @author gaoshuangwen
 * @since 2022/01/18 13:37
 */
public class ConfigHelper {

    private Config config;

    private Optional<String> getStringOpt(String key) {
        return getOpt(config::getString, key);
    }

    private Optional<Boolean> getBooleanOpt(String key) {
        return getOpt(config::getBoolean, key);
    }

    private Optional<Integer> getIntegerOpt(String key) {
        return getOpt(config::getInt, key);
    }

    private Optional<List<String>> getListOpt(String key) {
        return getOpt(config::getStringList, key);
    }

    private <T> Optional<T> getOpt(Function<String, T> func, String key) {
        return config.hasKey(key)
                ? Optional.ofNullable(func.apply(key))
                : Optional.empty();
    }

}
